<template>
  <div class="qualiy_list">
    <div class="monitor-tree">
      <h3 class="tree-title">选择隧道</h3>
      <el-tree
        :data="tunnelInfoChild"
        @check-change="handleCheckChange"
        :highlight-current="true"
        :accordion="true"
        :check-on-click-node="true"
        :default-expanded-keys="keysListNodeData"
        node-key="pid"
        ref="tree"
      ></el-tree>
    </div>
    <div class="monitor-list">
      <TunnelHeaderVue />
      <el-table
        :data="tunnelWidth"
        :cell-style="{'text-align':'center'}"
        :header-cell-style="{background: '#f8f8f9',textAlign: 'center'}"
      >
        <el-table-column width="100" show-overflow-tooltip prop="id" label="序号"></el-table-column>
        <el-table-column width="100" show-overflow-tooltip prop="line" label="洞幅名称"></el-table-column>
        <el-table-column width="100" show-overflow-tooltip prop="startMilestr" label="起始里程"></el-table-column>
        <el-table-column width="100" show-overflow-tooltip prop="endMilestr" label="终止里程"></el-table-column>
        <el-table-column width="200" show-overflow-tooltip prop="createTime" label="创建时间"></el-table-column>
        <el-table-column show-overflow-tooltip label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="currentPage.total"
      ></el-pagination>
    </div>
    <TunnelWidthEditor :flag="flag" />
  </div>
</template>

<script>
import api from "../../../api";
import qs from "qs";
import { mapState } from "vuex";
import TunnelWidthEditor from "./TunnelWidthEditor.vue";
import TunnelHeaderVue from "./TunnelWidthHeader.vue";
export default {
  data() {
    return {
      keysListNodeData:[],
      dId: "",
      tunnelInfoChild: [],
      tunnelWidth: [],
      flag: false,
      value: "--全部--",
      currentPage: {
        pageNo: 1,
        total: 0,
      },
    };
  },
  computed: {
    ...mapState("tunnelInfoModule", ["page", "search"]),
    searchChange() {
      return this.search;
    },
  },
  components: {
    TunnelWidthEditor,
    TunnelHeaderVue,
  },
  mounted() {
    this.getchildren();
    // 刷新操作
    this.$bus.$on("onRefreshtunnelWidth", (flag) => {
      this.http(1, flag);
    });
  },
  methods: {
    //获取数结构
    getchildren() {
      api.TunnelDirectionChild().then((res) => {
        if (res.data.code == 2001) {
          this.tunnelInfoChild = res.data.data
          const arrDat = res.data.data[0].pid
          this.keysListNodeData.push(arrDat)
        }
      });
    },
    http(page, dId) {
      api.tunnelWidthFindByTidPage({ page, dId }).then((res) => {
        if (res.data.code == 2001) {
          this.tunnelWidth = res.data.data.records;
          this.currentPage.total = parseInt(res.data.data.total);
        }
      });
    },
    handleEdit(index, row) {
      this.$bus.$emit("onShowTunnelWidthEditor", {
        rowData: row,
        dId: this.dId,
      });
    },
    handleDelete(index, row) {
      let data = {
        id: row.id,
      };
      this.$confirm("此操作将永久删除洞幅, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        customClass: "del-model",
        type: "warning",
      })
        .then(() => {
          api
            .tunnelWidthDeleteById(qs.stringify(data))
            .then((res) => {
              if (res.data.code == 2001) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.http(1, this.dId);
              } else {
                this.$message({
                  type: "fail",
                  message: "删除失败!",
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange(val) {
      this.http(val, this.dId);
    },
    handleCheckChange(data, checked, node) {
      if (data.did != undefined) {
        this.dId = data.did;
        this.http(1, this.dId);
        this.$bus.$emit("onAddTunnelWidthdId", this.dId);
      }
    },
  },
};
</script>

<style scoped>
.tree-title {
  text-align: left;
  font-size: 16px;
  color: #606266;
  padding: 10px;
  padding-bottom: 20px;
}

.monitor-tree {
  float: left;
  width: 300px;
  border-left: 1px solid #f1f1f1;
  border-right: 1px solid #f1f1f1;
  background-color: #fff;
}

.monitor-list {
  float: right;
  margin-left: 10px;
  width: 100%;
}

</style>